﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using LCA.Community.DAL.Interfaces;
using LCA.Community.DAL;

namespace LCA.Community.Cloud.DataLoader
{
    public class Program
    {
        private static IUnityContainer _unityContainer;

        static void Main(string[] args)
        {
            UnityContainerInitializer();
            var dataLoader = _unityContainer.Resolve<IDataLoader>(new ParameterOverrides { { "dataFolderPath", "Data" } });

            Console.WriteLine("Live Clinical Assistant data loader");
            Console.WriteLine("****************");
            Console.WriteLine("Choose an option:");
            Console.WriteLine("1. Delete all entities.");
            Console.WriteLine("2. Load all entities.");
            Console.WriteLine("3. Delete & Load entities.");
            Console.WriteLine("4. Exit.");
            Console.WriteLine("****************");

            var option = Console.ReadLine();

            switch(option)
            {
                case "1":
                    dataLoader.DeleteAll();
                    Console.WriteLine("All entities has been deleted.");
                    break;
                case "2":
                    dataLoader.LoadAll();
                    Console.WriteLine("All entities has been loaded.");
                    break;
                case "3":
                    dataLoader.DeleteAll();
                    Console.WriteLine("All entities has been deleted.");
                    dataLoader.LoadAll();
                    Console.WriteLine("All entities has been loaded.");
                    break;
                case "4":
                    break;
                default:
                    Console.WriteLine("Incorrect option.");
                    return;
            }

            Console.WriteLine("Exiting...");
            Console.ReadLine();
        }

        static void UnityContainerInitializer()
        {
            var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "Unity.config" };
            var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

            _unityContainer = new UnityContainer().LoadConfiguration(unitySection);
        }
    }
}
